package com.by_man.presenter.module_health_management.measure_blood_pressure;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


import com.android.volley.RequestQueue;
import com.by_man.presenter.R;
import com.by_man.presenter.SharedApplication;
import com.by_man.presenter.custom_layout.TimeDialogTool;
import com.by_man.repository.LocalDataManager;
import com.google.gson.Gson;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 手动输入血压信息Activity
 * Created by man on 2017-02-16.
 */
public class ManualInputBPActivity extends AppCompatActivity {
    private RequestQueue rQueue;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manual_input_blood_pressure);
        initView();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
//                Intent intent = new Intent(this, MeasureBloodPressureActivity.class);
//                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//                startActivity(intent);
                onBackPressed();
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            Intent intent = new Intent(this, MeasureBloodPressureActivity.class);
//            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//            startActivity(intent);
//        }
//        return super.onKeyDown(keyCode,event);
//    }

    private void initView() {
        Toolbar toolbar = (Toolbar)findViewById(R.id.tool_bar);
        //初始化ToolBar
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setHomeAsUpIndicator(R.drawable.icon_back);
        actionBar.setDisplayHomeAsUpEnabled(true);

        // 注册相关监听器
        final EditText highView=(EditText)findViewById(R.id.high);
        final EditText lowView=(EditText)findViewById(R.id.low);
        final EditText centerView=(EditText)findViewById(R.id.center);
        final EditText hrView=(EditText)findViewById(R.id.heart_rate);
        final EditText noteView = (EditText) findViewById(R.id.note);
        final TextView datanew = (TextView) findViewById(R.id.datanew);

        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        datanew.setText(sdf.format(calendar.getTime()));
        Log.i("Date", datanew.getText().toString());
        datanew.clearFocus();
        datanew.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TimeDialogTool tdt = new TimeDialogTool(ManualInputBPActivity.this, datanew);
                tdt.showDialog();
            }
        });

        Button save = (Button) findViewById(R.id.save);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String high,low,center,hr,note;
                high=highView.getText().toString().trim();
                low=lowView.getText().toString().trim();
                center=centerView.getText().toString().trim();
                hr=hrView.getText().toString().trim();
                note = noteView.getText().toString().trim();

                if (high.length() == 0 || low.length() == 0 ||  hr.length() == 0) {
                    SharedApplication.showToast(ManualInputBPActivity.this, "内容填写不正确，请重新输入");
                }else {
                    int i=Integer.parseInt(high);
                    int j=Integer.parseInt(low);
                    int k=Integer.parseInt(hr);
                    int m=0;
                    if(center.length() != 0){
                        m=Integer.parseInt(center);
                    }
                    if(i>300||i<30)
                    {
                        SharedApplication.showToast(ManualInputBPActivity.this, "收缩压内容填写不正确，请重新输入");
                        //showMyToast(toast,5000);  不明白设定这步的作用
                        highView.setText("");
                        highView. requestFocus ( ) ; //请求获取焦点
                    }else if(j>300||j<30) {
                        SharedApplication.showToast(ManualInputBPActivity.this, "舒张压内容填写不正确，请重新输入");
                        lowView.setText("");
                        lowView. requestFocus (); //请求获取焦点
                    }else if(k>300||k<30) {
                        SharedApplication.showToast(ManualInputBPActivity.this, "心率内容填写不正确，请重新输入");
                        hrView.setText("");
                        hrView.requestFocus();
                    }else if(i <= j) {
                        SharedApplication.showToast(ManualInputBPActivity.this,  "输入有误,请重新输入");
                        lowView.setText("");
                        lowView.requestFocus();
                    }else if(m != 0 && (m>300 || m<30)) {
                        SharedApplication.showToast(ManualInputBPActivity.this, "中心脉动压内容填写不正确，请重新输入");
                        centerView.setText("");
                        centerView.requestFocus();
                    }else if(i-j<11) {
                        SharedApplication.showToast(ManualInputBPActivity.this, "输入有误,请重新输入");
                        lowView.setText("");
                        lowView.requestFocus();
                    } else if(note.length() > 25) {
                        SharedApplication.showToast(ManualInputBPActivity.this, "备注字数过长,请重新输入");
                        //noteView.setText("");
                        noteView.requestFocus();
                    } else {
                        // 向服务器写入手动输入的血压心率数据
                        String number = datanew.getText().toString();
                        String str1 = number.substring(0, 4);
                        String str2 = number.substring(5, 7);
                        String str3 = number.substring(8, 10);
                        String str4 = number.substring(11, 17);
                        List<String> list = new ArrayList<String>();
                        StringBuffer sb = new StringBuffer();

                        sb.append(str1 + "-" + str2 + "-" + str3 + str4);

                        String time = sb.toString();
                        List<Map<String, Object>> testlist = new ArrayList<Map<String, Object>>();
                        Map<String, Object> test1 = new HashMap<String, Object>();
                        test1.put("id", 1);
                        test1.put("type", 0);
                        test1.put("value", high);
                        test1.put("time", time);
                        testlist.add(test1);
                        Map<String, Object> test2 = new HashMap<String, Object>();
                        test2.put("id", 2);
                        test2.put("type", 0);
                        test2.put("value", low);
                        test2.put("time", time);
                        testlist.add(test2);

                        Map<String, Object> test3 = new HashMap<String, Object>();
                        test3.put("id", 3);
                        test3.put("type", 0);
                        test3.put("value", center);
                        test3.put("time", time);
                        testlist.add(test3);

                        Map<String, Object> test4 = new HashMap<String, Object>();
                        test4.put("id", 4);
                        test4.put("type", 0);
                        test4.put("value", hr);
                        test4.put("time", time);
                        testlist.add(test4);

                        // 添加备注信息    2016-12-05   bm
                        Map<String, Object> noteInfo = new HashMap<String, Object>();
                        noteInfo.put("id", 7);
                        noteInfo.put("type", 0);
                        noteInfo.put("value", note);
                        noteInfo.put("time", time);
                        testlist.add(noteInfo);

                        Gson gson = new Gson();
                        String data = gson.toJson(testlist).toString();
                        Log.i("BP",data);
                        LocalDataManager.updateBloodPressureRecord(data);
                    }
                }
            }
        });
    }

//    protected void onRestart()
//    {
//        super.onRestart();
//        Intent intent=new Intent(this,GestureLockActivity.class);
//        intent.putExtra("class",this.getClass().getName());
//        intent.putExtra("position", 0);
//        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//        startActivity(intent);
//    }
}
